package com.leetcode.partition20;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/2/11 12:21
 */
public class LC1984学生分数的最小差值 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int k = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        int[] nums = new int[ss.length];
        for (int i = 0; i < ss.length; i++) nums[i] = Integer.parseInt(ss[i]);
        writer.write(minimumDifference(nums, k) + "\n");
        writer.flush();
    }

    public static int minimumDifference(int[] nums, int k) {
        int n = nums.length;
        Arrays.sort(nums, 0, n);
        int ans = nums[n - 1];
        for (int i = 0, j = k - 1; j < n; i++, j++) ans = Math.min(ans, nums[j] - nums[i]);
        return ans;
    }
}
